package com.cognizant.charts.linechart;

import java.awt.Color;
import java.text.NumberFormat;
import java.util.ArrayList;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardXYItemLabelGenerator;
import org.jfree.chart.labels.XYItemLabelGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeriesCollection;

import com.cognizant.charts.dataset.LineDataSet;

public class LineChart {
	private String chartTitle = "";
	private String xAxisLabel = "";
	private String yAxisLabel = "";
	private boolean showLegends = true;
	private boolean showToolTips = true;
	private Color chartBGColor = Color.white;
	private Color plotBGColor = Color.gray;
	private PlotOrientation plotOrientation = PlotOrientation.VERTICAL;
	private ArrayList<LineDataSet> dataCollection = new ArrayList<LineDataSet>();

	public void setChartTitle(String chartTitle) {
		this.chartTitle = chartTitle;
	}

	public void setxAxisLabel(String xAxisLabel) {
		this.xAxisLabel = xAxisLabel;
	}

	public void setyAxisLabel(String yAxisLabel) {
		this.yAxisLabel = yAxisLabel;
	}

	public void setShowLegends(boolean showLegends) {
		this.showLegends = showLegends;
	}

	public void setShowToolTips(boolean showToolTips) {
		this.showToolTips = showToolTips;
	}

	public void setChartBGColor(Color chartBGColor) {
		this.chartBGColor = chartBGColor;
	}

	

	public void setPlotBGColor(Color plotBGColor) {
		this.plotBGColor = plotBGColor;
	}

	public void setPlotOrientation(PlotOrientation plotOrientation) {
		this.plotOrientation = plotOrientation;
	}

	public void setDataCollection(ArrayList<LineDataSet> dataCollection) {
		this.dataCollection = dataCollection;
	}

	public JFreeChart createLineChart() {
		XYSeriesCollection dataset = new XYSeriesCollection();
		for (LineDataSet item : dataCollection) {
			dataset.addSeries(item.getXYSeries());
		}

		// Start creating the chart
		// create the chart...
		JFreeChart chart = ChartFactory.createXYLineChart(chartTitle, // chart
																		// title
				xAxisLabel, // x axis label
				yAxisLabel, // y axis label
				dataset, // data
				plotOrientation, showLegends, // include legend
				showToolTips, // tooltips
				false // urls
				);
		chart.setBackgroundPaint(chartBGColor);
		// get a reference to the plot for further customisation...
		XYPlot plot = (XYPlot) chart.getPlot();
		plot.setBackgroundPaint(plotBGColor);		
		XYLineAndShapeRenderer renderer
		= (XYLineAndShapeRenderer) plot.getRenderer();
		renderer.setShapesVisible(true);
		renderer.setShapesFilled(true);
		for (LineDataSet item : dataCollection) {
			renderer.setSeriesPaint(dataCollection.indexOf(item), item.getLineColor());
		}
		// label the points
        NumberFormat format = NumberFormat.getNumberInstance();
        format.setMaximumFractionDigits(2);
        XYItemLabelGenerator generator =
            new StandardXYItemLabelGenerator(
                StandardXYItemLabelGenerator.DEFAULT_ITEM_LABEL_FORMAT,
                format, format);
        renderer.setBaseItemLabelGenerator(generator);
        renderer.setBaseItemLabelsVisible(true);
		return chart;

	}

}
